package scct.chenlixian.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
//    数据库连接
private static String dbDriver="com.mysql.cj.jdbc.Driver";

//    url: jdbc:mysql://192.168.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8

    private static String dbUrl ="jdbc:mysql://localhost:3306/storedb?characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT%2B8";
    private  static  String name="root";
    private  static  String pass="123456";
    public static Connection getConnection()  {
        Connection conn=null;
        try{
            Class.forName(dbDriver);
            conn = DriverManager.getConnection(dbUrl,name, pass);

        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw  new RuntimeException(e);
        }
        return  conn;
    }


    public static void closeConnection(Connection conn) throws Exception {
        if (conn != null) {
           try{
               conn.close();
           }catch (SQLException e){
               throw  new RuntimeException(e);
           }
        }
    }


}
